from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.header import Header
import time
from threading import Thread

# 发送邮件
sender = 'shijian@hh-medic.com'
receiver = ['shmilyshijian@vip.qq.com', 'shasha@hh-medic.com']

host_server = 'smtp.qq.com'
pwd = 'sj010606'


def sendMail(info):
    message = create_msg(info)

    subject = '[办公室预订] by: ' + info.get('user_name') + '. 用途: ' + info.get('reason')[:50]

    smtp = SMTP_SSL(host_server)
    smtp.ehlo(host_server)
    smtp.login(sender, pwd)

    msg = MIMEText(message, "html", 'utf-8')
    msg["Subject"] = Header(subject, 'utf-8')
    msg["From"] = sender
    msg["To"] = Header('办公室预订管理员', 'utf-8')

    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()


def create_msg(info):
    now = time.strftime("%y年%m月%d号", time.localtime(time.time()))

    create_time = ''
    return """
            <table width="640" style="margin:0 auto;padding:0;border-collapse:collapse;border-spacing:0;border:1px solid #e0e0e0;border-radius:2px;background:#fff">
            <tbody>
            <tr>
            <td style="border-bottom:1px solid #ebebeb;padding:26px 0">
            <span style="float:right;font-size:16px;color:#999;margin-top:18px;margin-right:30px">{}</span>
            <img src="https://i.loli.net/2018/08/28/5b85123d1d392.png" width="60" height="60" style="margin:0 5px 0 30px;vertical-align:middle" title="meet.hh-medic.com，和缓办公室预订系统">
            <span title="meet.hh-medic.com，和缓办公室预订系统" style="font-size:24px;color:#4C97CF;text-decoration:none;vertical-align:middle">办公室预订</span>
            </td>
            </tr>

            <tr>
            <td>
            <table width="640" style="margin:0 auto;text-align:left;font-size:14px;color:#666;padding-top:25px">
            <tbody>
            <tr>
            <td style="vertical-align:text-top">
            <p style="font-size:14px;color:#666;text-align:center;width:80px">预订人</p>
            <p style="font-size:20px;color:#282828;line-height:1;margin:18px 0;text-align:center;width:80px">{}</p>
            <p style="font-size:14px;color:#666">
            </p>
            </td>

            <td style="vertical-align:text-top">
            <p style="font-size:14px;color:#666;text-align:center;width:100px">所属组</p>
            <p style="font-size:20px;color:#282828;line-height:1;margin:18px 0;text-align:center;width:100px">{}</p>
            <p style="font-size:14px;color:#666">
            </p>
            </td>

            <td style="vertical-align:text-top">
            <p style="font-size:14px;color:#666;text-align:center;width:180px">时间</p>
            <p style="font-size:14px;color:#282828;line-height:1;margin:18px 0;text-align:center;width:180px">{}</p>
            <p style="font-size:14px;color:#666">
            </p>
            </td>

            <td style="vertical-align:text-top">
            <p style="font-size:14px;color:#666;text-align:center">原因</p>
            <p style="font-size:14px;color:#282828;line-height:1;margin:18px 0;text-align:center;width:280px">{}</p>
            <p style="font-size:14px;color:#666">
            </p>
            </td>
            </tr>

            <tr>
            <td colspan="4">
            <hr style="width:480px;margin:0 auto;height:1px;background:#ebebeb;border:none;margin-top:11px">
            </td>
            </tr>

            <br>
            
            <tr>
            <td colspan="4" style="text-align:center">
            <br>
            <a href="https://meet.hh-medic.com/manage" rel="noopener" target="_blank" style="font-size:18px;color:white;text-decoration:none;border-radius:5px;padding:10px;background-color:#4C97CF">管理订单信息</a>
            <br>
            <br>
            </td>
            </tr>
            
            </tbody>
            </table>
            </td>
            </tr>
            
            </tbody>
            </table>
            
            """.format(now, info.get('user_name'), info.get('depart_name'), info.get('time_desc'), info.get('reason'))

